package Objetos 
{
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import Levels.Level;
	/**
	 * ...
	 * @author Rodrigo Forti
	 */
	public class Heroi extends MovieClip
	{
		
		public var NB_velocidadeY:Number;
		private var NB_velocidadeX:Number;
		private var NB_gravidade:Number;
		
		private var MC_tiro:Tiro;
		public var UI_vida:uint;
		
		public var BO_pegouUpgrade:Boolean;
		public var BO_pulando:Boolean;
		private var BO_dir:Boolean;
		private var BO_esq:Boolean;
		
		public var UI_player:uint;
		
		
		public function Heroi(player:uint) 
		{
			UI_player = player;
			UI_vida = 10;
			
			NB_velocidadeX = 8;
			NB_velocidadeY = 0;
			NB_gravidade = 0.9;
			
			Main.getInstance().stage.addEventListener(KeyboardEvent.KEY_DOWN, pressionar, false, 0, true);
			Main.getInstance().stage.addEventListener(KeyboardEvent.KEY_UP, soltar, false, 0, true);
		}
		
		public function pressionar (e:KeyboardEvent):void
		{	
			if (UI_player == 1)
			{
				switch (e.keyCode)
				{
					case Keyboard.D:
						BO_dir = true;
						if (this.scaleX < 0)
						{
							this.scaleX *= -1;
						}
						break;
						
					case Keyboard.A:
						BO_esq = true;
						if (this.scaleX > 0)
						{
							this.scaleX *= -1;
						}
						break;
						
					case Keyboard.SPACE:
						if (this.scaleX > 0)
						{
							MC_tiro = new Tiro("direita", UI_player,BO_pegouUpgrade);
						}
						else
						{
							MC_tiro = new Tiro("esquerda", UI_player,BO_pegouUpgrade);
						}
							MC_tiro.x = this.x;
							MC_tiro.y = this.y;
							Main.getInstance().MC_tela.addChild(MC_tiro);
							Level.AR_tiros.push(MC_tiro);
							break;
						
						
				}
				
				if (BO_pulando == false)
				{
					switch (e.keyCode)
					{
						case Keyboard.W:
							NB_velocidadeY = -15;
							BO_pulando = true;
							break;
					}
				}
			}
			
			if (UI_player == 2)
			{
				switch (e.keyCode)
				{
						case Keyboard.RIGHT:
							BO_dir = true;	
							if (this.scaleX < 0)
							{
								this.scaleX *= -1;
							}
							break;
							
						case Keyboard.LEFT:
							BO_esq = true;
							if (this.scaleX > 0)
							{
								this.scaleX *= -1;
							}
							break;
							
						case Keyboard.NUMPAD_0:
						if (this.scaleX > 0)
						{
							MC_tiro = new Tiro("direita",UI_player,BO_pegouUpgrade);
						}
						else
						{
							MC_tiro = new Tiro("esquerda",UI_player,BO_pegouUpgrade);
						}
							MC_tiro.x = this.x;
							MC_tiro.y = this.y;
							Main.getInstance().MC_tela.addChild(MC_tiro);
							Level.AR_tiros.push(MC_tiro);
							break;
				}
				
				if (BO_pulando == false)
				{
					switch (e.keyCode)
					{
						case Keyboard.UP:
							NB_velocidadeY = -15;
							BO_pulando = true;
							break;
					}
				}
			}
		}
		
		public function soltar (e:KeyboardEvent):void
		{
			if (UI_player == 1)
			{
				switch (e.keyCode)
				{
					case Keyboard.D:
						BO_dir = false;
						break;
					
					case Keyboard.A:
						BO_esq = false;
						break;
						
					case Keyboard.W:
						break;
				}
			}
			
			if (UI_player == 2)
			{
				switch (e.keyCode)
				{
					case Keyboard.RIGHT:
						BO_dir = false;
						break;
					
					case Keyboard.LEFT:
						BO_esq = false;
						break;
						
					case Keyboard.UP:
						break;
				}
			}
		}
		
		public function perderVida():void
		{
			UI_vida--;
		}
		
		public function removerListener():void
		{
			Main.getInstance().stage.removeEventListener(KeyboardEvent.KEY_UP, soltar);
			Main.getInstance().stage.removeEventListener(KeyboardEvent.KEY_DOWN, pressionar);
		}
		
		public function update():void
		{
			NB_velocidadeY += NB_gravidade;
			this.y += NB_velocidadeY;
			
			if (BO_dir)
			this.x += NB_velocidadeX;
			
			if (BO_esq)
			this.x -= NB_velocidadeX;
		}
		
	}

}